package cn.dubnation.car.controller;

import cn.dubnation.car.service.ICarCartService;
import cn.dubnation.car.domain.CarCart;
import cn.dubnation.car.query.CarCartQuery;
import cn.dubnation.base.util.AjaxResult;
import cn.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carCart")
public class CarCartController {
    @Autowired
    public ICarCartService carCartService;


    /**
     * 保存和修改公用的
     * @param carCart  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody CarCart carCart){
        if( carCart.getId()!=null)
            carCartService.update(carCart);
        else
            carCartService.insert(carCart);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        carCartService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        CarCart carCart = carCartService.queryById(id);
        return AjaxResult.me().setData(carCart);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){
        List< CarCart> list = carCartService.queryAll();
        return AjaxResult.me().setData(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody CarCartQuery query){
        PageInfo<CarCart> pageInfo = carCartService.queryPage(query);
        return AjaxResult.me().setData(pageInfo);
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        carCartService.batchDelete(ids);
        return AjaxResult.me();
    }
}
